package com.shiguangmao.util.statusbar;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

import com.shiguangmao.util.DensityUtil;
import com.shiguangmao.util.ScreenUtils;

/**
 * @ClassName: StatusBarUtil
 * @Description: java类作用描述 StatusBarUtil
 * @Author: shiguang.du
 * @CreateDate: 2021/5/27 15:57
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/27 15:57
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class StatusBarAndTitleUtil {
    private float offset = 0;

    /**
     * 去除状态栏，改变基础布局
     * 在CoordinateLayout中需要用这个
     *
     * @param activity
     * @param titleView
     */
    public static void removeStatusBar(Activity activity, View titleView) {
        removeStatusBar(activity, titleView, null, 44);
    }

    /**
     * 去除状态栏，改变基础布局
     * 在CoordinateLayout中需要用这个
     *
     * @param activity
     * @param titleView
     */
    public static void removeStatusBar(Activity activity, View titleView, int defaultHeight) {
        removeStatusBar(activity, titleView, null, defaultHeight);
    }

    /**
     * 去除状态栏，改变基础布局
     * 在CoordinateLayout中需要用这个
     *
     * @param activity
     * @param titleView
     * @param titleTextView
     */
    public static void removeStatusBar(Activity activity, View titleView, TextView titleTextView, int defaultHeight) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StatusBarUtil.setTransparent(activity);
            resetViewOnRemoveStatusBar(activity, titleView, titleTextView, defaultHeight);
        }
    }

    /**
     * 去除状态栏，改变基础布局
     * 在CoordinateLayout中需要用这个
     *
     * @param activity
     * @param titleView
     */
    public static void removeStatusBarInFragment(Activity activity, View titleView) {
        //默认为44dp
        removeStatusBarInFragment(activity, titleView, 44);
    }

    /**
     * 去除状态栏，改变基础布局
     * 在CoordinateLayout中需要用这个
     *
     * @param activity
     * @param titleView
     */
    public static void removeStatusBarInFragment(Activity activity, View titleView, int defaultHeight) {
        removeStatusBarInFragment(activity, titleView, null, defaultHeight);
    }

    /**
     * 去除状态栏，改变基础布局
     * 在CoordinateLayout中需要用这个
     *
     * @param activity
     * @param titleView
     * @param titleTextView
     */
    public static void removeStatusBarInFragment(Activity activity, View titleView, TextView titleTextView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StatusBarUtil.setTransparentForImageViewInFragment(activity, null);
            resetViewOnRemoveStatusBar(activity, titleView, titleTextView, 44);
        }
    }

    /**
     * 去除状态栏，改变基础布局
     * 在CoordinateLayout中需要用这个
     *
     * @param activity
     * @param titleView
     * @param titleTextView
     */
    public static void removeStatusBarInFragment(Activity activity, View titleView, TextView titleTextView, int defaultHeight) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StatusBarUtil.setTransparentForImageViewInFragment(activity, null);
            resetViewOnRemoveStatusBar(activity, titleView, titleTextView, defaultHeight);
        }
    }

    /**
     * 去除状态栏，改变基础布局
     * 在CoordinateLayout中需要用这个
     *
     * @param activity
     * @param titleView
     */
    public static void removeStatusBarInFragmentWithPadding(Activity activity, View titleView, int defaultHeight) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StatusBarUtil.setTransparentForImageViewInFragment(activity, null);
            int statusHeight = ScreenUtils.getStatusBarHeight(activity);
            if (titleView != null) {
                ViewGroup.LayoutParams titleLp = titleView.getLayoutParams();
                titleLp.height = DensityUtil.dip2px(activity, defaultHeight) + statusHeight;
                titleView.setLayoutParams(titleLp);
            }
//            if (titleView != null) {
//                titleView.setPadding(0, statusHeight, 0, 0);
//            }
        }
    }

    /**
     * 修改titleView布局
     * 整个Title 高度变高， 内容向下偏移
     *
     * @param activity
     * @param titleView
     * @param titleTextView
     */
    private static void resetViewOnRemoveStatusBar(Activity activity, View titleView, TextView titleTextView, int defaultHeight) {
        int statusHeight = ScreenUtils.getStatusBarHeight(activity);
        if (titleView != null) {
            ViewGroup.LayoutParams titleLp = titleView.getLayoutParams();
            titleLp.height = DensityUtil.dip2px(activity, defaultHeight) + statusHeight;
            titleView.setLayoutParams(titleLp);
        }
        if (titleTextView != null) {
            titleTextView.setPadding(0, statusHeight, 0, 0);
        }
    }

    /**
     * 移除statusBar，修改titleView布局
     * 整个Title 高度变高， 整体内容不偏移
     *
     * @param activity
     * @param titleView
     * @param titleTextView
     */
    private static void resetViewOnRemoveStatusBar(Activity activity, View titleView, TextView titleTextView) {
        int statusHeight = ScreenUtils.getStatusBarHeight(activity);
        if (titleView != null) {
            ViewGroup.LayoutParams titleLp = titleView.getLayoutParams();
            titleLp.height = DensityUtil.dip2px(activity, 44) + statusHeight;
            titleView.setLayoutParams(titleLp);
        }
        if (titleTextView != null) {
            titleTextView.setPadding(0, statusHeight, 0, 0);
        }
    }


    /**
     * recyclerview滚动改变title的透明度
     *
     * @param recyclerView
     * @param titleView    需要改变透明度的View
     * @param maxHeight    最大高度
     */
    public void setAlphaOnRecyclerViewScroll(RecyclerView recyclerView, View titleView, View textBg, int maxHeight) {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                offset += dy;
                if (offset == 0) {
                    titleView.setAlpha(0f);
                    if (textBg != null) {
                        textBg.setAlpha(0.6f);
                    }
                } else if (offset <= maxHeight) {
                    float alpha = offset / maxHeight;
                    titleView.setAlpha(alpha);
                    if (textBg != null) {
                        textBg.setAlpha(0.6f - alpha * 0.4f);
                    }
                } else {
                    if (titleView.getAlpha() != 1) {
                        titleView.setAlpha(1f);
                    }

                    if (textBg != null && textBg.getAlpha() != 0.2) {
                        textBg.setAlpha(0.2f);
                    }
                }
            }
        });
    }

    public void setAlphaOnRecyclerViewScroll(RecyclerView recyclerView, View titleView, int maxHeight) {
        setAlphaOnRecyclerViewScroll(recyclerView, titleView, null, maxHeight);
    }

    /**
     * 移除statusBar，修改titleView布局
     * 整个Title 高度变高， 整体内容不偏移
     *
     * @param activity
     * @param titleView
     * @param titleTextView
     */
    public static void removeStatusBarInFragmentBlack(Activity activity, View titleView, TextView titleTextView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StatusBarUtil.setTransparentForImageViewInFragmentBlack(activity, null);
            resetViewOnRemoveStatusBar(activity, titleView, titleTextView);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void setAlphaOnScrollViewScroll(ScrollView scrollView, View titleView, int maxHeight) {
        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY == 0) {
                    titleView.getBackground().mutate().setAlpha(0);
                } else if (scrollY <= maxHeight) {
                    int alpha = 255 * scrollY / maxHeight;
                    titleView.getBackground().mutate().setAlpha(alpha);
                } else {
                    if (titleView.getBackground().mutate().getAlpha() != 255) {
                        titleView.getBackground().mutate().setAlpha(255);
                    }
                }
            }
        });

        /*scrollView.set(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                offset += dy;
                if (offset == 0) {
                    titleView.setAlpha(0f);
                    if (textBg != null) {
                        textBg.setAlpha(0.6f);
                    }
                } else if (offset <= maxHeight) {
                    float alpha = offset / maxHeight;
                    titleView.setAlpha(alpha);
                    if (textBg != null) {
                        textBg.setAlpha(0.6f - alpha * 0.4f);
                    }
                } else {
                    if (titleView.getAlpha() != 1) {
                        titleView.setAlpha(1f);
                    }

                    if (textBg != null && textBg.getAlpha() != 0.2) {
                        textBg.setAlpha(0.2f);
                    }
                }
            }
        });*/
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void setAlphaOnNestedScrollViewScroll(NestedScrollView scrollView, View titleView, int maxHeight) {
        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY == 0) {
                    titleView.getBackground().mutate().setAlpha(0);
                } else if (scrollY <= maxHeight) {
                    int alpha = 255 * scrollY / maxHeight;
                    titleView.getBackground().mutate().setAlpha(alpha);
                } else {
                    if (titleView.getBackground().mutate().getAlpha() != 255) {
                        titleView.getBackground().mutate().setAlpha(255);
                    }
                }
            }
        });
    }
}
